package com.ymatou.iapi.optrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.req.DeliverReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.BargainGroupService;
import com.ymatou.iapi.optrade.service.SellerDeliveryMqCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sharding.model.OrderStatusLogPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;

/**
 * 虚拟商品通过MQ通知交易发货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SellerDeliveryMq {

	private static DeliverReq deliverReq;

	private static SellerDeliveryMqCall dispatchedcall;

	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("虚拟商品通过MQ通知交易发货接口");
	}

	@Before
	public void caseUp() {

		deliverReq = new DeliverReq();
		dispatchedcall = new SellerDeliveryMqCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(dispatchedcall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

//	/**
//	 * 校验自动确认收货时间操作详情表（auto_receive_opt_detail）
//	 */
//	private void verifyAutoReceiveDetail(boolean isFreeze) {
//
//		long orderId = deliverReq.getOrderId();
//
//		AutoReceiveOptDetailPo actual = orderShardingWapper
//				.selectAutoReceiveOptDetailByOrderId(orderId);
//
//		Logger.comment(String.format(
//				"验证自动确认收货时间操作详情表（auto_receive_opt_detail）  orderId: %s",
//				orderId));
//
//		Logger.verifyEquals(true,
//				Utils.calcTimeSpanMin(actual.getAddTime()) < 2,
//				"验证数据库AddTime跟系统当前时间差为2分钟内");
//		Logger.verifyEquals(true,
//				Utils.calcTimeSpanMin(actual.getUpdateTime()) < 2,
//				"验证数据库UpdateTime跟系统当前时间差为2分钟内");
//
//		int bizType = 1;
//		if (!deliverReq.isShippedByXlobo()) {
//			bizType = 2;
//		}
//
//		Logger.verifyEquals(orderId, actual.getOrderId(), "验证数据库OrderId");
//
//		Logger.verifyEquals(bizType, actual.getBizType(), "验证数据库BizType");
//
//		Logger.verifyEquals(null, actual.getUnfreezeTime(),
//				"验证数据库UnfreezeTime跟系统当前时间差为2分钟内");
//
//		Logger.verifyEquals(isFreeze, actual.getFreeze(), "验证数据库Freeze");
//
//		verifyAutoReceive(isFreeze);
//
//	}
	
//	/**
//	 * 校验自动确认收货时间操作表（auto_receive_opt） 
//	 */
//	private void verifyAutoReceive(boolean isFreeze) {
//
//		long orderId = deliverReq.getOrderId();
//		//  自动确认收货时间操作表（auto_receive_opt） 
//		AutoReceiveOptPo autoReceiveOptPo = orderShardingWapper
//				.selectAutoReceiveOptByOrderId(orderId);
//
//		Logger.comment(String.format(
//				"验证自动确认收货时间操作表（auto_receive_opt）  orderId: %s", orderId));
//
//		Logger.verifyEquals(true,
//				Utils.calcTimeSpanMin(autoReceiveOptPo.getAddTime()) < 2,
//				"验证数据库AddTime跟系统当前时间差为2分钟内");
//		Logger.verifyEquals(true,
//				Utils.calcTimeSpanMin(autoReceiveOptPo.getUpdateTime()) < 2,
//				"验证数据库UpdateTime跟系统当前时间差为2分钟内");
//
//		Logger.verifyEquals(orderId, autoReceiveOptPo.getOrderId(),
//				"验证数据库OrderId");
//		Logger.verifyEquals(isFreeze, autoReceiveOptPo.getFreeze(),
//				"验证数据库Freeze");
//
//		Logger.verifyEquals(true,
//				Utils.calcTimeSpanMin(autoReceiveOptPo.getFreezeTime()) < 2,
//				"验证数据库FreezeTime跟系统当前时间差为2分钟内");
//
//		/*		Logger.verifyEquals(null, autoReceiveOptPo.getExpectReceiveTime(),
//						"验证数据库ExpectReceiveTime");*/
//
//	}
	/**
	 * 校验订单表状态，单主状态操作日志表
	 */
	private void verifyOrderStatus(long orderId) {

		// 订单表(order)
		OrderPo ymtOrders = orderShardingWapper.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));

		Logger.verifyEquals(4, ymtOrders.getOrderStatus(), "验证orderStatus");
		Logger.verifyEquals(deliverReq.isShippedByXlobo(),
				ymtOrders.getXloboDelivery(), "验证xloboDelivery");

		Logger.verifyEquals(
				ymtOrders.getLogisticsType() == 7
						&& deliverReq.getSource() == 2,
				ymtOrders.getDomesticDelivery(), "验证domesticDelivery");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(ymtOrders.getDeliveryTime()) < 2,
				"验证数据库deliveryTime跟系统当前时间差为2分钟内");

		//订单主状态操作日志表(order_status_log)
		OrderStatusLogPo orderStatusLogPo = orderShardingWapper
				.selectOrderStatusLogByOrderId(orderId);

		Logger.comment("验证订单主状态操作日志表(order_status_log)");

		Logger.verifyEquals("确认收货", orderStatusLogPo.getMemo(), "验证数据库Memo");
		Logger.verifyEquals(4, orderStatusLogPo.getOrderStatus(),
				"验证数据库OrderStatus");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(orderStatusLogPo.getAddTime()) < 2,
				"验证数据库AddTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, orderStatusLogPo.getOrderId(),
				"验证数据库OrderId");
		Logger.verifyEquals(-1L, orderStatusLogPo.getOperator(),
				"验证数据库Operator");

	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SellerDelivery_001() {
		Logger.start(true, "虚拟商品通过MQ通知交易成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = BargainGroupService
					.placeVirtualProduct();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());

			TradingCallService.sellerAcceptOrder(placeOrderResp.getOrderList()
					.get(0).getSellerId(), orderId);

			Logger.comment("前置数据准备结束");

			Thread.sleep(3000);

			// 订单ID
			deliverReq.setOrderId(orderId);

			dispatchedcall.setData(deliverReq);
			dispatchedcall.callService();

			Logger.verifyEquals("ok", dispatchedcall.getReturnData(), "验证接口返回");

			verifyOrderStatus(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
